package com.koron.system.datadict.controller;

import com.koron.bean.base.Response;
import com.koron.common.core.business.datadict.bean.po.SysDataDictDetail;
import com.koron.common.core.business.datadict.service.SysDataDictDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 系统管理-数据字典详情{@code SysDataDictDetail}控制层
 *
 * <p>主要提供：增加、删除、修改、查询功能</p>
 *
 * @author 朱超
 * @since 2021-08-26
 */
@RestController
@RequestMapping("sys/DataDictDetail")
@Api(tags = "系统管理-数据字典详情接口")
public class SysDataDictDetailController {
    private final SysDataDictDetailService sysDataDictDetailService;

    public SysDataDictDetailController(SysDataDictDetailService sysDataDictDetailService) {
        this.sysDataDictDetailService = sysDataDictDetailService;
    }

    /**
     * 新增单条数据
     *
     * @param sysDataDictDetail:至少包含：dictionary,appid,key,value,sort
     * @return Response对象
     * @see Response
     * @version 1.0
     */
    @PostMapping
    @ApiOperation(value = "新增单条数据字典详情")
    public Response insert(@RequestBody @Valid SysDataDictDetail sysDataDictDetail) {
        return ADOConnection.runTask(sysDataDictDetailService, "checkBeforeInsert", Response.class, sysDataDictDetail);
    }

    /**
     * 删除数据
     *
     * @param findId 主键
     * @return Response对象
     * @see Response
     * @version 1.0
     */
    @DeleteMapping
    @ApiOperation(value = "根据id删除单条数据字典详情")
    public Response delete(@RequestParam("findId") String findId) {
        return ADOConnection.runTask(sysDataDictDetailService, "deleteById", Response.class, findId);
    }

    /**
     * 修改数据
     *
     * @param sysDataDictDetail 实体对象
     * @return Response对象
     * @see Response
     * @version 1.0
     */
    @PutMapping
    @ApiOperation(value = "修改单条数据字典详情", notes = "id为必填项")
    public Response checkBeforeUpdate(@RequestBody SysDataDictDetail sysDataDictDetail) {
        return ADOConnection.runTask(sysDataDictDetailService, "checkBeforeUpdate", Response.class, sysDataDictDetail);
    }

    /**
     * 查询所有的数据字典，及其关联的数据字典详情
     *
     * @return Response对象
     * @see Response
     * @version 1.0
     */
    @GetMapping("/all")
    @ApiOperation(value = "查询所有的数据字典，及其关联的数据字典详情")
    public Response getDictionaryAndDictionaryDetails() {
        return ADOConnection.runTask(sysDataDictDetailService, "getDictionaryAndDictionaryDetails", Response.class);
    }

    /**
     * 根据数据字典和所属水司查找对应的数据字典详情信息
     *
     * @param dictionary-数据字典id
     * @param appid-租户id
     * @return Response对象
     * @see Response
     * @version 1.0
     */
    @GetMapping("getDetailsByDictionaryAndAppId")
    @ApiOperation(value = "根据数据字典和所属水司查找对应的数据字典详情信息")
    public Response getDetailsByDictionaryAndAppId(@RequestParam String dictionary, String appid) {
        return ADOConnection.runTask(sysDataDictDetailService, "getDetailsByDictionaryAndAppId", Response.class, dictionary, appid);
    }

    /**
     * 根据classifyId，code，appid，key，来查询对应的数据
     *
     * @param classifyId
     * @param code
     * @param appid
     * @param key
     * @param factory
     * @return Response对象
     * @see Response
     * @version 1.0
     */
    @GetMapping("getByClassifyIdAndCodeAndAppidAndKey")
    @ApiOperation("根据classifyId，code，appid，key，来查询对应的数据")
    public Response getByClassifyIdAndCodeAndAppidAndKey(@RequestParam String classifyId, @RequestParam String code,
                                                         @RequestParam String appid, @RequestParam String key, SessionFactory factory) {
        SysDataDictDetail sysDataDictDetail = sysDataDictDetailService.getByClassifyIdAndCodeAndAppidAndKey(factory,classifyId,code,appid,key);
        return Response.success(sysDataDictDetail);
    }
}